package com.bjpowernode.web.controller;

import com.bjpowernode.api.model.SysUser;
import com.bjpowernode.api.result.DubboResult;
import com.bjpowernode.api.vo.ProductBidInfo;
import com.bjpowernode.contacts.YLBRedis;
import com.bjpowernode.enums.Code;
import com.bjpowernode.web.views.ResultView;
import com.bjpowernode.web.vo.InvestRank;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: panDeng_Zhu
 * @time: 2022/10/24 21:29
 */
@CrossOrigin
@Api(tags = "投资操作")
@RestController
public class InvestController extends BaseController {

    /*投资排行榜*/
    @ApiOperation(value = "投资排行榜", notes = "投资金额最多的三个用户，显示手机号，投资金额")
    @GetMapping("/v1/invest/ranking")
    public ResultView queryInvestRanking() {
        ResultView<InvestRank> resultView = ResultView.SUCCESS();

        List<InvestRank> rank = new ArrayList<>();
        //取数据，reverse表示按score从高到低排序获取数据
        Set<ZSetOperations.TypedTuple<String>> sets = stringRedisTemplate.opsForZSet().
                reverseRangeWithScores(YLBRedis.INVEST_RANKING, 0, 2);
        sets.forEach(s -> {
            rank.add(new InvestRank(s.getValue(), s.getScore()));
        });
        //设置ResultView的数据
        resultView.setList(rank);
        return resultView;
    }


    @ApiOperation(value = "投资产品", notes = "客户购买理财产品")
    @PostMapping("/v1/invest/product")
    public ResultView investProduct(@RequestHeader("userId") Integer userId,
                                    @RequestParam("productId") Integer productId,
                                    @RequestParam("money") Integer money) {
        ResultView<String> result = ResultView.FAIL();
        //判断请求参数
        if (userId != null && productId != null && money != null) {
            //判断是否实名认证
            SysUser user = userService.queryByUserId(userId);
            if (user != null && StringUtils.isNotBlank(user.getName())) {
                //可以投资
                DubboResult dubboResult = investService.investProduct(userId, productId, money);
                if (dubboResult.isInvoke()) {
                    //更新投资排行榜，更新ZSet类新的数据，value=手机号，score=累计的投资金额
                    stringRedisTemplate.opsForZSet().incrementScore(YLBRedis.INVEST_RANKING, user.getPhone(), money);
                    //投资成功
                    result = ResultView.SUCCESS();
                } else {
                    result.setMsg(dubboResult.getMsg());
                }
            } else {
                result.setEnumCode(Code.REQUIRED_REALNAME);
            }
        } else {
            result.setEnumCode(Code.PARAMETER_NULL);
        }
        return result;
    }

    @ApiOperation(value = "分页查询投资记录", notes = "客户查询最近的投资记录表")
    @GetMapping("/v1/invest/info")
    public ResultView investInfo(@RequestHeader("userId") Integer userId,
                                 @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        ResultView<ProductBidInfo> result = ResultView.FAIL();
        if (userId >0) {
                List<ProductBidInfo> list=investService.queryBidListByUserId(userId,pageNo,pageSize);
                result=ResultView.SUCCESS();
                result.setList(list);
        }
        return result;
    }
}


